//商品列表的主模块

require.config({
    baseUrl: "./modules",
    paths: {
        "jq": "../libs/jquery",
        "pt": "../libs/jquery.pagination",
        "cookie": "../libs/jquery.cookie",
    },
    shim: {
        cookie: { deps: ["jq"] }, pt: { deps: ["jq"] }
    }
})
require(["cookie", "pt", "getGoods", "listPaging", "setLocal"], (cookie, pt, getGoods, listPaging, setLocal,) => {
    //请求数据
    const serUrl = "http://localhost:3000/api";
    getGoods(serUrl, "getGoods", res => {
        listPaging($(".Product"), res);
        //搜索功能
        $(document).keyup(function () {
            if (event.keyCode === 13) {
                // console.log($("input[class=one]").val())
                // console.log(res)
                const keyWord = $("input[class=one]").val();
                const Arr = res.filter(val => {
                    return val.name.includes(keyWord)
                });
                listPaging($(".Product"), Arr)
            }
        })
        //排序功能
        // 价格排序
        const Arr1 = [...res]
        let flag = true;
        $(".2li").on("click", function () {
            if (flag) {
                Arr1.sort((a, b) => a.price - b.price)
                listPaging('', Arr1);
            }
            else {
                Arr1.sort((a, b) => b.price - a.price)
                listPaging('', Arr1);
            }
            flag = !flag
         })
        // 默认排序
        $(".1li").on("click", function () {
            // Arr1.sort((a, b) => a.price - b.price)
            listPaging('', res);
        })
        //新品排序
        $(".3li").on("click", function () {
            const Arr2 = [];
            Arr1.forEach((item)=>{
                
                if(item.new !== undefined){
                    Arr2.push(item);
                }
                // console.log(item.new)
            })
            // console.log(Arr2)
            listPaging('', Arr2);
        })
    });


    //绑定事件
    $(".Product").on("click", ".add", function () {
        // console.log($(this).attr("data-id"))
        const id = $(this).attr("data-id");
        // 此处获取当前商品价格的方式，受不同页面布局的影响而改变
        const price = $(this).parent().prev("p").prev("p").html() - 0;
        // 4. 将当前商品相关信息，存储到本地存储
        setLocal(id, price);
    });
    

    //头尾页面
    $(".top1").load("http://localhost:3000/html/public.html #top");
    $(".bottom1").load("http://localhost:3000/html/public.html #footer");
    //登录守卫
	
  
})

